<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style></style>
<body>
		
</body>
<script>
	
	// 场景一: 根据status显示对应名称
	const statusStr = new Map([
		['1', ['待付款']],
		['2', ['待发货']],
		['3', ['已发货']],
		['4', ['交易完成']],
		['5', ['交易关闭']],
		['default', ['']]
	])

	const getStatus = (status) => {
		let actions = statusStr.get(status) || statusStr.get('default')
		return actions[0]
	}

	// 场景二: 多个condition对应名称
	const actions = new Map([
		[{identity: 'guest', status: 1}, () => {/*do sth*/}],
		[{identity: 'guest', status: 2}, () => {/*do sth*/}]
	])

	const onButtonClick = (identity, status) => {
		let action = [...actions].filter(([key, value]) => {
			key.identity === identity && key.status === status
		})
		action.forEach(([key, value]) => value())
	}

	// 场景三: 根据status做出相应操作
	/** 
	 	orderType: 订单类型,1.500元定金用户,2.200元定金用户,3.普通用户
		pay: 是否已支付 true/false (已下单但未支付用户需进入普通购买模式)
		stock: 产品库存,已支付定金用户不受限
	*/
	// 职责链模式
	let order500 = (orderType, pay, stock) => {
		if (orderType === 1 && pay === true) {
			console.log('500元定金预购,得到100优惠券')
		} else {
			order200(orderType, pay, stock)
		}
	}
	let order200 = (orderType, pay, stock) => {
		if (orderType === 2 && pay === true) {
			console.log('200元定金预购,得到50优惠券')
		} else {
			orderNormal(orderType, pay, stock)
		}
	}
	let orderNormal = (orderType, pay, stock) => {
		if (stock > 0) {
			console.log('普通购买,无优惠券')
		} else {
			console.log('手机库存不足')
		}
	}
	order500(1, true, 500)

</script>
</html>